﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using TransControlSystem.Dao;
using TransControlSystem.Model;
using TransControlSystem.ViewModel.Common;
using TransControlSystem.ViewModel.Common.Command;

namespace TransControlSystem.ViewModel
{
    /// <summary>
    /// 优先级配置后台逻辑
    /// </summary>
    public class PriorityConfigViewModel : ViewModelBase
    {
        /// <summary>
        /// 优先级队列
        /// </summary>
        private ObservableCollection<PriorityConfig> _PriorityConfigList;
        /// <summary>
        /// 优先级队列
        /// </summary>
        public ObservableCollection<PriorityConfig> PriorityConfigList
        {
            get
            {
                return _PriorityConfigList;
            }
            set
            {
                _PriorityConfigList = value;
                RaisePropertyChanged("PriorityConfigList");
            }
        }
        /// <summary>
        /// 提升优先级命令
        /// </summary>
        public DelegateCommand PromotionPriorityCommand { get; set; }
        /// <summary>
        /// 降低优先级命令
        /// </summary>
        public DelegateCommand ReductionPriorityCommand { get; set; }
        /// <summary>
        /// 隐藏窗体命令
        /// </summary>
        public DelegateCommand HidPriorityConfigWindowCommand { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        public PriorityConfigViewModel()
        {
            #region 初始化命令
            PromotionPriorityCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(PromotionPriorityHandler)
            };
            ReductionPriorityCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ReductionPriorityHandler)
            };
            HidPriorityConfigWindowCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(HidPriorityConfigHandler)
            };
            #endregion 

            PriorityConfigList = new ObservableCollection<PriorityConfig>();
            RefreshConfigList();
        }

        /// <summary>
        /// 隐藏窗体处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void HidPriorityConfigHandler(object obj)
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HidPriorityConfigWindow"));
        }

        /// <summary>
        /// 级别数组
        /// </summary>
        private string[] level = new string[5] { "A", "B", "C", "D", "E" };

        /// <summary>
        /// 提升优先级处理程序
        /// </summary>
        /// <param name="obj">参数：被调级别在数据库中Id的值</param>
        private void PromotionPriorityHandler(object obj)
        {
            try
            {
                //获取要调的级别
                List<PriorityConfig> DataPriorityConfigList = DapperTools.Query<PriorityConfig>("select a.* from PriorityConfig AS a left join PriorityConfig AS b on a.Priority = b.Priority where b.Id =" + obj + " order by a.Priority,PriorityNo");
                if (DataPriorityConfigList.Count > 0)
                {
                    if (DataPriorityConfigList[0].Priority != "A")
                    {
                        //当前级
                        string CurrentPriority = DataPriorityConfigList[0].Priority;
                        //获取上一级的计划update成当前级
                        //获取上一级
                        string UpOneLevel = level[Array.IndexOf(level, CurrentPriority) - 1];
                        List<PriorityConfig> UpOneLevelList = DapperTools.Query<PriorityConfig>("select a.* from PriorityConfig AS a  where a.Priority ='" + UpOneLevel + "' order by a.Priority,PriorityNo");
                        //把上一级变成当前级别
                        for (int i = 0; i < UpOneLevelList.Count; i++)
                        {
                            UpOneLevelList[i].Priority = CurrentPriority;
                        }
                        DapperTools.Update<PriorityConfig>("update PriorityConfig set Priority = @Priority where Id = @Id", UpOneLevelList);
                        //把当前级变成上一级
                        for (int i = 0; i < DataPriorityConfigList.Count; i++)
                        {
                            DataPriorityConfigList[i].Priority = UpOneLevel;
                        }
                        DapperTools.Update<PriorityConfig>("update PriorityConfig set Priority = @Priority where Id = @Id", DataPriorityConfigList);
                        LogTool.SaveLog("优先级配置", "【" + DataPriorityConfigList[0].TagType + "】的优先级从【" + CurrentPriority + "】→【" + level[Array.IndexOf(level, CurrentPriority) - 1] + "】提升了优先级。");
                        RefreshConfigList();
                    }
                }
            }
            catch (Exception e)
            {
                LogTool.SaveSysLog(e.Message, "提升优先级异常");
            }
        }
        /// <summary>
        /// 降低优先级处理程序
        /// </summary>
        /// <param name="obj">参数：被调级别数据库中Id的值</param>
        private void ReductionPriorityHandler(object obj)
        {
            try
            {
                //获取要调的级别
                List<PriorityConfig> DataPriorityConfigList = DapperTools.Query<PriorityConfig>("select a.* from PriorityConfig AS a left join PriorityConfig AS b on a.Priority = b.Priority where b.Id =" + obj + " order by a.Priority,PriorityNo");
                if (DataPriorityConfigList.Count > 0)
                {
                    if (DataPriorityConfigList[0].Priority != "E")
                    {
                        //当前级
                        string CurrentPriority = DataPriorityConfigList[0].Priority;
                        //获取下一级的计划update成当前级
                        //获取下一级
                        string NextOneLevel = level[Array.IndexOf(level, CurrentPriority) + 1];
                        List<PriorityConfig> NextOneLevelList = DapperTools.Query<PriorityConfig>("select a.* from PriorityConfig AS a  where a.Priority ='" + NextOneLevel + "' order by a.Priority,PriorityNo");
                        //把下一级变成当前级别
                        for (int i = 0; i < NextOneLevelList.Count; i++)
                        {
                            NextOneLevelList[i].Priority = CurrentPriority;
                        }
                        DapperTools.Update<PriorityConfig>("update PriorityConfig set Priority = @Priority where Id = @Id", NextOneLevelList);
                        //把当前级变成下一级
                        for (int i = 0; i < DataPriorityConfigList.Count; i++)
                        {
                            DataPriorityConfigList[i].Priority = NextOneLevel;
                        }
                        DapperTools.Update<PriorityConfig>("update PriorityConfig set Priority = @Priority where Id = @Id", DataPriorityConfigList);
                        LogTool.SaveLog("优先级配置", "【" + DataPriorityConfigList[0].TagType + "】的优先级从【" + CurrentPriority + "】→【" + level[Array.IndexOf(level, CurrentPriority) + 1] + "】降低了优先级。");
                        RefreshConfigList();
                    }
                }
            }
            catch (Exception e)
            {
                LogTool.SaveSysLog(e.Message, "降低优先级异常");
            }
        }


        /// <summary>
        /// 刷新列表
        /// </summary>
        private void RefreshConfigList()
        {
            List<PriorityConfig> DataList = DapperTools.Query<PriorityConfig>("select * from PriorityConfig ORDER BY Priority,PriorityNo");
            if (PriorityConfigList.Count == 0)
            {
                for (int i = 0; i < DataList.Count; i++)
                {
                    PriorityConfigList.Add(DataList[i]);
                }
            }
            else
            {
                PriorityConfigList.Clear();
                for (int i = 0; i < DataList.Count; i++)
                {
                    PriorityConfigList.Add(DataList[i]);
                }
            }
        }
    }
}
